package Amap;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author LIBOFEI
 * @ClassName HttpUtil
 * @description
 * @date 2021/6/30 13:02
 * @Version 1.0
 **/
public class HttpUtil {



    public HttpUtil() {
    }

    public static String doGet(String url, String queryString) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url + "?" + queryString);
        CloseableHttpResponse response = null;

        String var7;
        try {
            response = httpClient.execute(httpGet);
            if (response == null || response.getStatusLine().getStatusCode() != 200) {

                return null;
            }

            HttpEntity resEntity = response.getEntity();
            String result = EntityUtils.toString(resEntity, "utf-8");

            var7 = result;
        } catch (Exception var23) {
            var23.printStackTrace();
            return null;
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException var22) {
                    var22.printStackTrace();
                }
            }

            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException var21) {
                    var21.printStackTrace();
                }
            }

        }

        return var7;
    }

    public static String doPost(String url, Map<String, String> params) {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> pairs = new ArrayList();
        Iterator var5 = params.entrySet().iterator();

        while(var5.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry)var5.next();
            pairs.add(new BasicNameValuePair((String)entry.getKey(), (String)entry.getValue()));
        }

        CloseableHttpResponse response = null;

        String var8;
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
            response = httpClient.execute(httpPost);
            if (response == null || response.getStatusLine().getStatusCode() != 200) {

                return null;
            }

            HttpEntity resEntity = response.getEntity();
            String result = EntityUtils.toString(resEntity, "utf-8");

            var8 = result;
        } catch (Exception var24) {
            var24.printStackTrace();
            return null;
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException var23) {
                    var23.printStackTrace();
                }
            }

            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException var22) {
                    var22.printStackTrace();
                }
            }

        }

        return var8;
    }

    public static String doPostJson(String url, String jsonStr) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json");
        CloseableHttpResponse response = null;

        try {
            StringEntity se = new StringEntity(jsonStr, "utf-8");
            se.setContentType("text/json");
            se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
            httpPost.setEntity(se);
            response = httpClient.execute(httpPost);
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntity = response.getEntity();
                String result = EntityUtils.toString(resEntity, "utf-8");

                String var8 = result;
                return var8;
            }


        } catch (Exception var24) {
            var24.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException var23) {
                    var23.printStackTrace();
                }
            }

            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException var22) {
                    var22.printStackTrace();
                }
            }

        }

        return null;
    }
}
